let container = document.querySelector('.uu')
// let list = document.querySelector('.list')
let foot = document.querySelector('.footer')
let span = document.querySelector('span')
function render(data, el) {
    // console.log(data, el)
    let domStr = ''
    data.forEach(item => {
        // let li = document.createElement('li')
        let regExp = /(?<=imgurl=)[^'"]*/
        let result = item.productImg2.match(regExp)
        result = result[0]
        console.log(result)
        domStr += `<li productid=${item.productId}><img src="${result}" alt=""><b>${item.productName}</b>
            <b>${item.productComCount}</b><b>立即抢购</b></li>`
        el.innerHTML = domStr
        // el.appendChild(li)
        // list.appendChild(el)
    })
}
function getData(pageid) {
    fetch(`http://chst.vip:1234/api/getmoneyctrl?pageid=${pageid}`)
        .then(body => body.json())
        .then(res => {
            console.log(res)
            render(res.result, container)
        })
}
function lazyLoad(fn, threshold = 200) {
    let flag = true;
    window.onscroll = function () {
        let scrollHeight = document.documentElement.scrollHeight;
        let clientHeight = document.documentElement.clientHeight;
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        let m = scrollHeight - scrollTop - clientHeight;

        if (m <= threshold) {
            if (flag) {
                flag = false;
                fn()
            }
        } else {
            flag = true;
        }
    }
}
getData()
let pageid = 0
lazyLoad(function () {
    pageid++
    if (pageid > 14) {
        return
    } else {
        getData(pageid)
    }
}, 50)

container.onclick=function(e){
    console.log(e.target)
    if(e.target.localName == 'li'){
        let _this = e.target
        let id = _this.getAttribute('productid')
        console.log(id);
        location.href = "./../html/indexxiang.html?productid="+id
        id = Number(id)
        // console.log(id)
    }
}


span.onclick = function () {
    location.href = "./../index.html"
}
foot.onclick = function () {
    let scrollTop = document.documentElement.scrollTop
    console.log(scrollTop)
    document.documentElement.scrollTop = 0;
}